// 自启动小红书app
auto.waitFor();
app.launch("com.xingin.xhs");
// 点击购物
console.log("点击购物");
desc('购物').click();
// 等待页面加载
sleep(1500);
// 点击搜索
text('搜索').click();
// 清空输入框();
desc('全部删除').click();
// 输入关键词
console.log("输入关键词");
setText("办公桌");
text('搜索').click();
// 等待页面加载
sleep(1500);

// 捕获第一张卡片的尺寸 className('FrameLayout')，depth(18)className('android.view.ViewGroup')
try {
    // 查找第一张商品卡片
    let firstCard =className('FrameLayout').depth(16).findOne(3000);
    
    if (firstCard) {
        // 获取卡片的边界信息
        let bounds = firstCard.bounds();
        
        console.log("第一张卡片信息:");
        console.log("位置: (" + bounds.left + ", " + bounds.top + ", " + bounds.right + ", " + bounds.bottom + ")");
        console.log("宽度: " + (bounds.right - bounds.left));
        console.log("高度: " + (bounds.bottom - bounds.top));
        console.log("中心点: (" + bounds.centerX() + ", " + bounds.centerY() + ")");
        
        // 获取卡片高度用于滚动
        let cardHeight = bounds.bottom - bounds.top + 87;
        
        // 向上滚动一个卡片的高度
        console.log("向上滚动卡片高度: " + cardHeight + "像素");
        let screenWidth = device.width;
        let startX = screenWidth / 2;
        let startY = device.height * 0.7;  // 从屏幕70%位置开始
        let endX = startX;
        let endY = startY - cardHeight;    // 向上滚动卡片高度的距离
        
        swipe(startX, startY, endX, endY, 500);
        console.log("滚动完成，等待页面稳定...");
        sleep(1000);
        
        // 捕获第二张卡片的边界信息
        console.log("开始捕获第二张卡片信息...");
        try {
            let secondCard = className('FrameLayout').depth(16).find();
            if (secondCard.length >= 2) {
                let secondBounds = secondCard[1].bounds();
                console.log("第二张卡片信息:");
                console.log("位置: (" + secondBounds.left + ", " + secondBounds.top + ", " + secondBounds.right + ", " + secondBounds.bottom + ")");
                console.log("宽度: " + (secondBounds.right - secondBounds.left));
                console.log("高度: " + (secondBounds.bottom - secondBounds.top));
                console.log("中心点: (" + secondBounds.centerX() + ", " + secondBounds.centerY() + ")");
            } else {
                console.log("未找到第二张卡片");
            }
        } catch (error) {
            console.log("捕获第二张卡片时出错:", error);
        }
        
        // 点击第一张卡片
        console.log("点击第一张商品卡片");
        firstCard.click();
        
    } else {
        console.log("未找到符合条件的商品卡片");
    }
    
} catch (error) {
    console.log("捕获卡片尺寸时出错:", error);
}